package GUI.Administracion;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import employee.entities.EmployeeVO;
import employee.exceptions.IncompleteEmployeeException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.employee.interfaces.EmployeeProxyMgt;

import org.eclipse.wb.swt.SWTResourceManager;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;

public class AgregarEmpleado extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	private Text text_1;
	private Text text_2;
	Label error;
	private Text text_3;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AgregarEmpleado(Shell parent, int style) {
		super(parent, style);
		setText("Agregar Empleado");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 300);
		shell.setText(getText());
		shell.setLayout(null);
		
		Label lblNombre = new Label(shell, SWT.NONE);
		lblNombre.setBounds(33, 48, 59, 14);
		lblNombre.setText("Nombre");
		
		Label lblApellido = new Label(shell, SWT.NONE);
		lblApellido.setBounds(33, 87, 59, 14);
		lblApellido.setText("Apellido");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(33, 159, 101, 14);
		lblNewLabel.setText("Tipo de Empleado");
		
		Label lblContrasenaDeLogin = new Label(shell, SWT.NONE);
		lblContrasenaDeLogin.setBounds(33, 195, 130, 14);
		lblContrasenaDeLogin.setText("Contrasena de login");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(177, 45, 162, 19);
		
		text_1 = new Text(shell, SWT.BORDER);
		text_1.setBounds(177, 84, 162, 19);
		
		text_2 = new Text(shell, SWT.BORDER);
		text_2.setBounds(177, 192, 170, 19);
		
		final Combo combo = new Combo(shell, SWT.NONE);
		combo.setBounds(177, 155, 162, 22);
		combo.add("Cocina");
		combo.add("Mozo");
		combo.add("Cajero");
		combo.add("administrativo");
		
		Button btnIngresarNuevoEmpleado = new Button(shell, SWT.NONE);
		btnIngresarNuevoEmpleado.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String nombre = null;
				if(text.getText().equalsIgnoreCase("")){
					error.setText(error.getText() + " Es necesario Agregar un Nombre de Empleado");
				}
				else{
				nombre=text.getText();	
				}
				
				String cedula = text_3.getText();
				
				String apellido =null;
				if(text_1.getText().equalsIgnoreCase("")){
					error.setText(error.getText() + " Es Necesario Agregar un Apellido de Empleado");
				}
				apellido = text_1.getText();
				int op=0;
				if(combo.getText().equalsIgnoreCase("Mozo")){
					op=0;
				}
				
				if(combo.getText().equalsIgnoreCase("Cajero")){
					op=1;
				}
				if(combo.getText().equalsIgnoreCase("Cocina")){
					op=2;
				}
				if(combo.getText().equalsIgnoreCase("Administrativo")){
					op=3;
				}
				String cont = null;
				if(text_2.getText().equalsIgnoreCase("")){
					error.setText(error.getText() + " es necesario ingresar una contrasena");
				}
				else{
					cont=text_2.getText();
				}
				
				if(cont!=null && nombre!= null && apellido!=null && combo.getSelection()!=null){
					EmployeeVO emp = new EmployeeVO(cont, nombre,apellido,op);
					EmployeeProxyMgt em = ServiceFacade.getInstance().getEmployeeProxyMgt();
					
					try {
						em.add(emp);
						shell.dispose();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (IncompleteEmployeeException e1) {
						error.setText("Ingrese todos los datos pedidos");
						e1.printStackTrace();
					}
				}
				
				
			}
		});
		btnIngresarNuevoEmpleado.setBounds(111, 240, 228, 28);
		btnIngresarNuevoEmpleado.setText("Ingresar Nuevo Empleado");
		
		error = new Label(shell, SWT.NONE);
		error.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		error.setBounds(33, 195, 386, 14);
		error.setText("");
		
		Label lblCedulaDeIdentidad = new Label(shell, SWT.NONE);
		lblCedulaDeIdentidad.setBounds(33, 125, 112, 14);
		lblCedulaDeIdentidad.setText("Cedula de Identidad");
		
		text_3 = new Text(shell, SWT.BORDER);
		text_3.setBounds(177, 122, 162, 19);
	
		/*
		 *private static final long serialVersionUID = 1L;
			private String password, name, lastName;
			private int id;
			private int employeeType;

		 */

	}
}
